import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

function sleep(time) {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve();
    }, time);
  });
}
export default () => {
  const store = new Vuex.Store({
    state: {
      name: "张三",
      age: 18,
    },
    mutations: {
      changeName(state, payload) {
        state.name = payload;
      },
    },
    actions: {
      changeName({ commit }, payload) {
        console.log("p-----", payload);
        return new Promise((resolve, reject) => {
          setTimeout(() => {
            commit("changeName", payload);
            resolve();
          }, 4000);
        });
      },
    },
    getters: {},
  });

  // state 转换
  if (typeof window !== "undefined" && window.__INITIAL_STATE__) {
    store.replaceState(window.__INITIAL_STATE__);
  }
  return store;
};
